package controller

import (
	"context"
	authApi "mldong/api/auth"
	"mldong/api/base"
	"mldong/internal/modules/sys/logic"
	"mldong/internal/modules/sys/service"
	"mldong/utility"

	"github.com/gogf/gf/v2/frame/g"
)

func init() {
	// 注册控制器时自动注入依赖
	utility.RegisterController(&AuthController{
		// userService: utility.MustResolve(service.UserServiceType).(service.IUserService),
		// 控制层使用强依赖logic的方式，保证逻辑层的init方法比controller的方法先执行
		userService: utility.MustResolve(service.UserServiceType).(*logic.UserLogic),
	})
}

type AuthController struct {
	userService service.IUserService
}

// 登录
func (ctrl *AuthController) Login(ctx context.Context, req *authApi.LoginReq) (res *authApi.LoginRes, err error) {
	// 设置 JSON 数据
	loginVO, err := ctrl.userService.Login(ctx, req)
	if err != nil {
		return
	}
	g.RequestFromCtx(ctx).Response.WriteJson(base.Data(loginVO))
	return
}

// 退出
func (ctrl *AuthController) Logout(ctx context.Context, req *authApi.LogoutReq) (res *base.CommonResult, err error) {
	g.RequestFromCtx(ctx).Response.WriteJson(base.Ok())
	return
}

// 获取登录图片验证码是否启用标识
func (ctrl *AuthController) CaptchaOpenFlagReq(ctx context.Context, req *authApi.CaptchaOpenFlagReq) (res *authApi.CaptchaOpenFlagRes, err error) {
	var flag bool
	errr := utility.GetSysConfig("MOLE_CAPTCHA_OPEN", &flag, false)
	if errr != nil {
		flag = false
	}
	vo := authApi.CaptchaOpenFlagVO{Flag: flag}
	g.RequestFromCtx(ctx).Response.WriteJson(base.Data(vo))
	return
}
